#!/usr/bin/with-contenv sh

set -u # Treat unset variables as an error.

# Handle the VNC password.
if [ -f /root/.vncpass ] && [ -n "$( cat /root/.vncpass )" ]; then
    VNC_SECURITY="-rfbauth /root/.vncpass"
else
    VNC_SECURITY="-nopw"
fi

# Handle IPv6 options.
if ifconfig -a | grep -wq inet6; then
    IPV6="-rfbportv6 5900"
else
    IPV6="-rfbportv6 -1 -no6 -noipv6 -httpportv6 -1"
fi

# Handle connection encryption options.
if [ "${SECURE_CONNECTION:-0}" -eq 1 ]; then
    VNC_SECURITY="$VNC_SECURITY -stunnel /config/certs/vnc-server.pem"
fi

echo "[$(basename "$(pwd)")] starting..."

exec 2>&1 /usr/bin/x11vnc  \
                    -display $DISPLAY \
                    -rfbport 5900 \
                    $IPV6 \
                    -forever \
                    -desktop "${APP_NAME}" \
                    -cursor arrow \
                    -shared \
                    $VNC_SECURITY \
                    ${X11VNC_EXTRA_OPTS:- }

# vim: set ft=sh :
